<?php

class UserRaoVatInfoController extends BackEndController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array(
                'allow',
                'actions' => array('login', 'logout'),
                'users' => array('*'),
            ),
            array(
                'allow',
                'actions' => array('index', 'view', 'create', 'update', 'admin', 'delete'),
                'users' => array('@'),
            ),

            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new UserRaoVatInfo;

        if (isset($_POST['UserRaoVatInfo'])) {
            $model->attributes = $_POST['UserRaoVatInfo'];

            $model->join_time = time();

            $file = CUploadedFile::getInstance($model, 'image');
            if ($file != null) {
                $imageVariables = getimagesize($file->tempName);
                $file = EUploadedImage::getInstance($model, 'image');
                $arr_size = TreTodayCommon::getResizeImage($imageVariables[0], $imageVariables[1]);
                $file->maxWidth = $arr_size['width'];
                $file->maxHeight = $arr_size['height'];
                $rnd = rand(0, 999999);
                $fileName = "{$rnd}.jpg";
                $base = TreTodayCommon::createFolder('avata');

                $base = $base . '/' . $fileName;
                if ($file->saveAs($base)) {
                    $model->avata_url = TreTodayCommon::generateImagePath('avata', $fileName);
                    $model->save();
                }
            } else {
                $model->avata_url = "";
            }
            if ($model->save())
                $this->redirect(array('index'));
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['UserRaoVatInfo'])) {
            $model->attributes = $_POST['UserRaoVatInfo'];
            $file = CUploadedFile::getInstance($model, 'image');
            if ($file != null) {
                $imageVariables = getimagesize($file->tempName);
                $file = EUploadedImage::getInstance($model, 'image');
                $arr_size = TreTodayCommon::getResizeImage($imageVariables[0], $imageVariables[1]);
                $file->maxWidth = $arr_size['width'];
                $file->maxHeight = $arr_size['height'];
                $rnd = rand(0, 999999);
                $fileName = "{$rnd}.jpg";
                $base = TreTodayCommon::createFolder('avata');

                $base = $base . '/' . $fileName;
                if ($file->saveAs($base)) {
                    $model->avata_url = TreTodayCommon::generateImagePath('avata', $fileName);
                    $model->save();
                }
            }
            if ($model->save())
                $this->redirect(array('index'));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionIndex() {
        $model = new UserRaoVatInfo('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['UserRaoVatInfo']))
            $model->attributes = $_GET['UserRaoVatInfo'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = UserRaoVatInfo::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionLogin() {
        $this->redirect(TreTodayUrl::backendLoginUrl());
    }

    public function actionLogout() {
        $this->redirect(TreTodayUrl::backendLogoutUrl());
    }

}
